package com.wl.java_spring_boot01.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.wl.java_spring_boot01.modules.account.entity.Role;
import com.wl.java_spring_boot01.modules.account.service.RoleService;
import com.wl.java_spring_boot01.modules.common.vo.Result;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 汪林
 * @date 2020/8/22 17:52
 */
@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 127.0.0.1/api/roles ---- get
     */
    @GetMapping("/roles")
    public List<Role> getRoles() {
        return roleService.getRoles();
    }

    /**用户页面分页数据
     * 127.0.0.1/api/roles ---- post
     * {"currentPage":"1","pageSize":"5","keyWord":"张"}
     */
    @PostMapping(value = "/roles", consumes = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<Role> getUsersBySearchVo(@RequestBody SearchVo searchVo){
        return roleService.getUsersBySearchVo(searchVo);
    }

    /**
     * 127.0.0.1/api/role ---- post
     * {"roleName":"大白兔"}
     */
    @PostMapping(value = "/role", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Role> insertRole(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    /**查询
     * 127.0.0.1/api/role/1 ---- get
     */
    @GetMapping(value = "/role/{roleId}")
    public Role getRoleByRoleId(@PathVariable int roleId){
        return roleService.getRoleByRoleId(roleId);
    }

    /**修改
     * 127.0.0.1/api/role ---- put
     * {"roleId":"1","roleName":"zl"}
     */
    @PutMapping(value = "/role", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Role> updateUser(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    /**删除--参数放在path路径使用@PathVariable
     * 127.0.0.1/api/role/1 ---- delete
     */
    @DeleteMapping(value = "/role/{roleId}")
    public Result<Object> deleteRole(@PathVariable int roleId){
        return roleService.deleteRole(roleId);
    }
}
